package au.com.handsunited.web.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.User;
import au.com.handsunited.service.UserService;
import au.com.handsunited.web.command.user.UserSearchCommand;

/**
 * 
 * @author arunda
 */
public class UserSearchController extends SimpleFormController {

	private UserService userService;

	private static Logger logger = LoggerFactory.getLogger(UserSearchController.class);


	/**
	 *
	 */
	public UserSearchController() {
		setCommandClass(UserSearchCommand.class);
	}

	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		UserSearchCommand userSearchCommand = (UserSearchCommand) command;

		if (logger.isDebugEnabled()) {
			logger.debug("User search fields [firstName={}, lastName={}, emailAddress={}",
					new String[] {userSearchCommand.getFirstName(), userSearchCommand.getLastName(), userSearchCommand.getEmailAddress()});
		}

		List<User> users = userService.searchUsers(
				userSearchCommand.getFirstName(), userSearchCommand.getLastName(), userSearchCommand.getEmailAddress());

		ModelAndView mav = new ModelAndView(getSuccessView(), "userSearch", userSearchCommand);
		mav.addObject("users", users);
		return mav;
	}


	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
